package com.chapter.sample.jvm.gc.escape;

import java.util.concurrent.TimeUnit;

/**
 * 栈上分配测试
 * -Xms1G -Xmx1G -XX：-DoEscapeAnalysis -XX：+PrintGCDetails
 * -Xms1G -Xmx1G -XX:-DoEscapeAnalysis -Xlog:gc* -XX:+UseParallelGC
 * 只要开启了逃逸分析，就会判断方法的变量是否发生了逃逸。如果发生了逃逸，则会使用栈上分配
 */
public class StackAllocation {
    public static void main(String[] args) {
        long start = System.currentTimeMillis();
        for (int i = 0; i <= 10000000; i++) {
            alloc();
        }
        long end = System.currentTimeMillis();
        System.out.println("花费的时间为：" + (end - start) + "ms");
        //为了查看堆内存中对象的个数，线程sleep
        try {
            TimeUnit.SECONDS.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    public static void alloc() {
        User user = new User();
    }
    static class User { }
}


